package com.rzjm.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.CityDao;
import com.rzjm.api.entity.City;
import com.rzjm.api.service.CityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CityServiceImpl extends BaseServiceImpl<CityDao, City> implements CityService {

    @Resource
    private CityDao cityDao;

    @Override
    public List<Map<String, Object>> getCityListByPinyinGroup(String keyword) {
        List<Map<String, Object>> list = new ArrayList<>();
        String[] characters = new String[]{ "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" };
        for (String c : characters){
            QueryWrapper<City> qw = new QueryWrapper<>();
            qw.eq("name_character",c);
            if(StrUtil.isNotEmpty(keyword)){
                qw.like("city_name",keyword);
            }
            List<City> estates = cityDao.selectList(qw);
            if(CollUtil.isNotEmpty(estates)){
                Map<String, Object> map = new HashMap<>();
                map.put("key",c);
                map.put("value",estates);
                list.add(map);
            }
        }
        return list;
    }

}
